<?php

/** @var \Laravel\Lumen\Routing\Router $router */

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It is a breeze. Simply tell Lumen the URIs it should respond to
| and give it the Closure to call when that URI is requested.
|
*/

use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;

$router->get('.well-known/webfinger', function (Request $request) use ($router) {
	$id = array();
	preg_match("/acct:(.+)@.+/", $request->input('resource'), $id);
	$id = $id[1];
	$userdir = '../userdata/'.$id;
	$webfingerfile = $userdir.'/webfinger.json';
	if (!file_exists($userdir)) {
		abort(404);
	} else {
		$myfile = fopen($webfingerfile, "r");
		$content = fread($myfile,filesize($webfingerfile));
		fclose($myfile);
		return response($content);
	}
});


$router->get('user/{id}', function ($id) use ($router) {
	$userdir = '../userdata/'.$id;
	$webfingerfile = $userdir.'/webfinger.json';
	if (!file_exists($userdir)) {
		abort(404);
	} else {
		$myfile = fopen($webfingerfile, "r");
		$content = fread($myfile,filesize($webfingerfile));
		fclose($myfile);
		return response($content);
	}
});

$router->get('/', function () use ($router) {
	return Http::withHeaders([
	'Accept' => 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
	])->get('https://floss.social/api/v1/timelines/public');
});

